﻿$(function () {
    //初步诊断
    $("#pathway").ligerComboBox({
        width: 150,
        data: [{ "ID": 0, "Name": "胸痛" }, { "ID": 1, "Name": "脑卒中" }],
        //url: '/PatientList/GetPathNameList?r=' + new Date().getMilliseconds(),
        isMultiSelect: false,
        valueField: "ID",
        textField: "Name",
        value: '0',
        valueFieldID: 'hdnpathway',
        onSuccess: function (d) { }
    });
    //患者状态
    $("#status").ligerComboBox({
        width: 80,
        data: [{ "ID": -1, "Name": "全部" }, { "ID": 0, "Name": "院前" }, { "ID": 1, "Name": "在科" }],
        isMultiSelect: false,
        valueField: "ID",
        textField: "Name",
        value: '-1',
        valueFieldID: 'hdnstatus',
        onSuccess: function (d) { }
    });
    //来院方式
    $("#comeType").ligerComboBox({
        width: 110,
        data: [{ "ID": 0, "Name": "全部" }, { "ID": 1, "Name": "120" }, { "ID": 2, "Name": "转院" }, { "ID": 3, "Name": "自行来院" }, { "ID": 4, "Name": "院内发病" }],
        isMultiSelect: false,
        valueField: "ID",
        textField: "Name",
        value: '0',
        valueFieldID: 'hdncomeType',
        onSuccess: function (d) { }
    });
    $('#btnSearch').click(function () {
        search(1, $("select[name=rp]").val());
    });
    $("#btnPrintWd").on("click", function () {
        if ($("#txtSelectedpId").val() == 0) { $.ligerDialog.alert('请选择要操作的患者信息'); return; }
        var tit = "腕带打印";
        $.ligerDialog.open({
            height: 400,
            width: 700,
            title: tit,
            url: '/ChestPainCenter/PatientList/WristPrint?pid=' + $("#txtSelectedpId").val(),
            showMax: false,
            showToggle: false,
            showMin: false,
            isResize: true,
            slide: false,
            data: {},
            buttons: [{
                text: '回调函数', onclick: function (i, d) {
                    $("#btnSearch").click();
                }
            }],
            cls: "closeCls"
        }).max();
        $(".l-dialog-buttons").hide();
    });
    $('#btnGiveup').click(function () {
        if ($("#txtSelectedpId").val() == 0) { $.ligerDialog.alert('请选择要操作的患者信息'); return; }
        $.ligerDialog.confirm('确定作废选定患者吗', function (yes) {
            if (yes) {
                var url = "/ChestPainCenter/PatientList/GiveUp";
                $.get(url, {
                    rnd: new Date().getMilliseconds(),
                    patientId: $("#txtSelectedpId").val()
                }, function (data, textStatus) {
                    if (textStatus == "success") {
                        var d = $.parseJSON(data);
                        if (d.Result == 1) {
                            $.ligerDialog.success("操作成功");
                            $('#btnSearch').click();
                        }
                        else if (d.Result == 0) {
                            $.ligerDialog.error(d.Body);
                        }
                        else {
                            $.ligerDialog.error("程序错误");
                        }
                    }
                })
            }
        });
    });
    $('#btnAdd').click(function () {
        edit(0);
    });
    $("#btnEdit").on("click", function () {
        if ($("#txtSelectedpId").val() == 0) { $.ligerDialog.alert('请选择要操作的患者信息'); return; }
        edit($("#txtSelectedpId").val())
    })
    $('#btnSearch').click();
});
$(document).keyup(function (event) {
    if (event.keyCode == 13) {
        $('#btnSearch').click();
    }
});
function opennewtab(patientId, patientName) {
    window.parent.top.tab.addTabItem({ tabid: patientId, text: "胸痛病历", url: "/ChestPainCenter/PathRecord/Index?patientId=" + patientId });
}
function search(pageNumber,pageSize) {
    var queryData = {
        name: $("#txtName").val(),
        status: $("#hdnstatus").val(),
        pathway: $("#hdnpathway").val(),
        cometype: $("#hdncomeType").val()
    }
    initTable(queryData, 1, pageSize);
}
function initTable(queryData, pageNumber, pageSize) {
    $("#maingrid").find("tbody").children().remove();
    $("#maingrid").ligerGrid({
        height: '100%',
        data: {
            "Total": 16, "Rows": [{ "ID": 260, "PATIENT_ID": "N2435678", "IsGreenChannel": null, "PatCardNO": "", "PATIENT_NAME": "张志明", "PATIENT_SEX": "男", "PATIENT_AGE": "21", "IS_BINDING_WRISTBAND": null, "RFID_State": false, "TagID_State": false, "TriageCode_State": false, "PATIENT_STATUS": 0, "EMS_ARRIVEDOOR_TIME": "2019-06-20 17:21:00", "PATIENT_ARRIVE_CLASS": "2", "PRE_DIAGNOSIS": null, "DiagnosticPathId": 2, "DiagnosticPathName": "<span style=\"color:green\">胸痛<span> | <span style=\"color:green\">创伤<span>", "ApprovalStatus": null, "ApprovalRemark": null },
                { "ID": 247, "PATIENT_ID": "5521144", "IsGreenChannel": 1, "PatCardNO": "3301809351", "PATIENT_NAME": "李勇", "PATIENT_SEX": "女", "PATIENT_AGE": "31", "IS_BINDING_WRISTBAND": null, "RFID_State": false, "TagID_State": true, "TriageCode_State": true, "PATIENT_STATUS": 0, "EMS_ARRIVEDOOR_TIME": "2019-04-01 00:00:00", "PATIENT_ARRIVE_CLASS": "3", "PRE_DIAGNOSIS": null, "DiagnosticPathId": 11, "DiagnosticPathName": "<span style=\"color:green\">胸痛<span> | <span style=\"color:green\">脑卒中<span>", "ApprovalStatus": 3, "ApprovalRemark": "" },
                { "ID": 248, "PATIENT_ID": "1610201904104614", "IsGreenChannel": null, "PatCardNO": "", "PATIENT_NAME": "陈一鸣", "PATIENT_SEX": "男", "PATIENT_AGE": "33", "IS_BINDING_WRISTBAND": null, "RFID_State": false, "TagID_State": false, "TriageCode_State": false, "PATIENT_STATUS": 0, "EMS_ARRIVEDOOR_TIME": null, "PATIENT_ARRIVE_CLASS": "", "PRE_DIAGNOSIS": null, "DiagnosticPathId": 0, "DiagnosticPathName": "<span style=\"color:green\">胸痛<span>", "ApprovalStatus": null, "ApprovalRemark": null }], "InHospitalNum": 0, "PreHospitalNum": 16, "PathWayNum": 2
        },
        //url: '/PatientList/Search?ram=' + new Date().getMilliseconds(),
        parms: queryData,
        page: pageNumber,
        pageSize: IsEmpty(pageSize) ? 20 : pageSize,
        rownumbers: false,
        heightDiff: -5,
        columns: [
            { display: '就诊ID', name: 'PATIENT_ID', minWidth: 50, width: 150, },
            {
                display: '姓名', name: 'PATIENT_NAME', minWidth: 100, width: 120, render: function (row) {
                    return "<a style=\"cursor:pointer;\" onclick=\"opennewtab('" + row.PATIENT_ID + "','" + row.PATIENT_NAME + "')\" >" + row.PATIENT_NAME + "</a>";
                }
            },
            {
                display: '性别', name: 'PATIENT_SEX', minWidth: 50, width: 80,
                render: function (rowData) {
                    if (typeof (rowData.PATIENT_SEX) == "undefined" || rowData.PATIENT_SEX == null || rowData.PATIENT_SEX == "-1")
                        return "未知";
                    if (rowData.PATIENT_SEX == "0")
                        return "男";
                    if (rowData.PATIENT_SEX == "1")
                        return "女";
                    return "<span title=\"" + rowData.PATIENT_SEX + "\">" + rowData.PATIENT_SEX + "</span>";
                }
            },
            { display: '年龄', name: 'PATIENT_AGE', minWidth: 50, width: 80, },
            {
                display: '腕带状态', name: 'RFID_State', minWidth: 100, width: 80,
                render: function (rowData) {
                    if (typeof (rowData.RFID_State) == "undefined" || rowData.RFID_State == null)
                        return "未知";
                    if (rowData.RFID_State == "0")
                        return "未绑定";
                    if (rowData.RFID_State == "1")
                        return "<span style=\"color:green\">已绑定</span>";
                    return "<span title=\"" + rowData.RFID_State + "\">未知</span>";
                }
            },
            {
                display: '患者状态', name: 'PATIENT_STATUS', minWidth: 50, width: 80,
                render: function (rowData) {
                    if (typeof (rowData.PATIENT_STATUS) == "undefined" || rowData.PATIENT_STATUS == null)
                        return "未知";
                    if (rowData.PATIENT_STATUS == "0")
                        return "院前";
                    if (rowData.PATIENT_STATUS == "1")
                        return "在科";
                    return "<span title=\"" + rowData.PATIENT_STATUS + "\">未知</span>";
                }
            },
            {
                display: '绿色通道', name: 'IsGreenChannel', minWidth: 50, width: 80,
                render: function (rowData) {
                    if (typeof (rowData.IsGreenChannel) == "undefined" || rowData.IsGreenChannel == null || rowData.IsGreenChannel == "0")
                        return "<span onclick=\"StartGreenChannel(" + rowData.PATIENT_ID + ")\" style=\"cursor:pointer\">未启动</span>";
                    if (rowData.IsGreenChannel == "1")
                        return "<span style=\"color:green\">已启动</span>";
                    return "<span onclick=\"StartGreenChannel(" + rowData.PATIENT_ID + ")\" style=\"cursor:pointer\">未启动</span>";
                }
            },
            {
                display: '到达医院时间', name: 'EMS_ARRIVEDOOR_TIME', minWidth: 150, width: 180,
                render: function (rowData) {
                    var time = rowData.EMS_ARRIVEDOOR_TIME;
                    return obj2Timemm(time);
                }
            },
            {
                display: '来院方式', name: 'PATIENT_ARRIVE_CLASS', minWidth: 100, width: 180,
                render: function (rowData) {
                    if (typeof (rowData.PATIENT_ARRIVE_CLASS) == "undefined" || rowData.PATIENT_ARRIVE_CLASS == null || rowData.PATIENT_ARRIVE_CLASS == "0")
                        return "未知";
                    if (rowData.PATIENT_ARRIVE_CLASS == "1")
                        return "120";
                    if (rowData.PATIENT_ARRIVE_CLASS == "2")
                        return "转院";
                    if (rowData.PATIENT_ARRIVE_CLASS == "3")
                        return "自行来院";
                    if (rowData.PATIENT_ARRIVE_CLASS == "4")
                        return "院内发病";
                    return "<span title=\"" + rowData.PATIENT_ARRIVE_CLASS + "\">不祥</span>";
                }
            },
            {
                display: '诊疗路径', name: 'DiagnosticPathName', minWidth: 100,
                render: function (row) {
                    return row.DiagnosticPathName;
                }
            }
        ],
        isScroll: true,
        onSuccess: function (data, grid) {
            $("#spanPatientNum").text(data.Total);
            $("#spanInHospitalNum").text(data.InHospitalNum);
            $("#spanPreHospitalNum").text(data.PreHospitalNum);
            $("#txtSelectedpId").val(0);
        },
        onSelectRow: function (data, rowindex, rowobj) {
            $("#txtSelectedpId").val(data.PATIENT_ID);
        }
    });
}
function StartGreenChannel(patientId) {
    $.ligerDialog.confirm('确定启动绿色通道', function (yes) {
        if (yes) {
            $.post("/ChestPainCenter/GreenChannel/StartGreenChannel", {
                rnd: new Date().getMilliseconds(),
                patientId: patientId
            }, function (data, textStatus) {
                if (textStatus == "success") {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {                         
                        $.ligerDialog.success("启动成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                }
            })
        }
    });
}
function showPath(id) {
    $.ligerDialog.open({
        height: 650,
        width: 420,
        title: '路径总览',
        url: '/ChestPainCenter/Pathway/Index?id=' + id,
        showMax: true,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: {},
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnSearch").click();
            }
        }],
        cls: "closeCls"
    }).max();
    $(".l-dialog-buttons").hide();
}
function edit(id) {
    var tit = (!IsEmpty(id) && parseInt(id) > 0) ? "编辑患者" : "新增患者";
    $.ligerDialog.open({
        height: 470,
        width: 520,
        title: tit,
        url: '/ChestPainCenter/PatientList/Edit?id=' + id,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: {},
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnSearch").click();
            }
        }],
        cls: "closeCls"
    }).max();
    $(".l-dialog-buttons").hide();
}
$(document).on("click", ".closeCls", function () {
    $("#btnSearch").click();
});